package com.atguigu.controller;


import com.atguigu.entity.CartInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.CartInfoService;
import com.atguigu.util.AuthContextHolder;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 购物车表 用户登录系统时更新冗余 前端控制器
 * </p>
 *
 * @author fumeng
 * @since 2021-11-11
 */
@RestController
@RequestMapping("/cart")
public class CartInfoController {

    @Autowired
    CartInfoService cartInfoService;

    @PostMapping("addToCart/{skuId}/{skuNum}")
    public RetVal addToCart(@RequestParam Long skuId, @RequestParam Integer skuNum, HttpServletRequest request) {
        // 如何获取到userId，在网关中将用户Id{登录，未登录}传递到后台 从header中获取登录的userId
        String userId = AuthContextHolder.getUserId(request);
        // 说明用户未登录
        if (StringUtils.isEmpty(userId)) {
            // 未登录：获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.addToCart(skuId, userId, skuNum);
        return RetVal.ok();
    }

    //购物车列表显示
    @GetMapping("getCartList")
    public RetVal getCartList(HttpServletRequest request) {
        //拿到用户id
        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);

        List<CartInfo> cartInfoList =cartInfoService.getCartList(userId,userTempId);
        return RetVal.ok(cartInfoList);
    }

    //http://api.gmall.com/cart//checkCart/27/1
    //商品的选中
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public RetVal checkCart(@PathVariable Long skuId,@PathVariable Integer isChecked,HttpServletRequest request) {
        //拿到用户id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId=AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.checkCart(userId,skuId,isChecked);
        return RetVal.ok();
    }

    // http://api.gmall.com/cart//deleteCart/28
    //删除购物车内商品
    @DeleteMapping("deleteCart/{skuId}")
    public RetVal deleteCart(@PathVariable Long skuId,HttpServletRequest request){
        //拿到用户id
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
            userId=AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCart(userId,skuId);
        return RetVal.ok();
    }
    //5.查询选中的商品信息
    @GetMapping("getSelectedProduct/{userId}")
    public List<CartInfo> getSelectedProduct(@PathVariable String userId){
        return cartInfoService.getSelectedProduct(userId);
    }

    //6.从数据库中查询出最新的购物车信息到redis中
    @GetMapping("queryFromDbToRedis/{userId}")
    public List<CartInfo> queryFromDbToRedis(@PathVariable String userId){
        return cartInfoService.queryFromDbToRedis(userId);
    }
}

